package eraserhead.xml.response;

import org.w3c.dom.Node;

import xml.Message;
import client.IMessageHandler;
import eraserhead.boundary.DecisionGUI;
import eraserhead.entity.Decision;
import eraserhead.entity.Edge;
import eraserhead.xml.XmlNode;

/**
 * Response handler for AddEdge
 * @author Michael Checca
 */
public class AddEdgeResponse implements IMessageHandler {

	@Override
	public void process(Message response) {
		Node addEdge = XmlNode.findNode(response.contents, "addEdgeResponse");
		if (addEdge != null) {
			Decision model = DecisionGUI.getInstance().getModel();
			String id = XmlNode.getAttribute(addEdge, "id");
			int left = Integer.parseInt(XmlNode.getAttribute(addEdge, "left"));
			int right = Integer.parseInt(XmlNode.getAttribute(addEdge, "right"));
			int height = Integer.parseInt(XmlNode.getAttribute(addEdge, "height"));
			model.addEdge(new Edge(left, right, height));
			DecisionGUI.getInstance().getLinesCanvas().repaint();
			System.out.println(String.format("AddEdgeResponse received [id: %s, left: %d, right: %d, height: %d]",
					id, left, right, height));
		}
	}
}
